package com.house.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.house.common.context.BaseContext;
import com.house.model.dto.HistoryDto;
import com.house.model.entity.History;
import com.house.model.vo.HouseVo;
import com.house.service.mapper.HistoryMapper;
import com.house.service.service.IHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class HistoryServiceImpl implements IHistoryService {

    @Autowired
    private HistoryMapper historyMapper;

    @Override
    public void addHistory(String houseId) {
        String userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<History> delelteW = new LambdaQueryWrapper<>();
        delelteW.eq(History::getUserId,userId);
        delelteW.eq(History::getHouseId,houseId);

        // 先删除在新增
        historyMapper.delete(delelteW);
        History history = History.builder()
                .userId(userId)
                .houseId(houseId)
                .createDate(new Date())
                .createPerson(userId)
                .build();
        historyMapper.insert(history);
    }

    @Override
    public List<HouseVo> getHistory() {
        String userId = BaseContext.getCurrentId();
        List<HouseVo> houseVos = historyMapper.selectHouseVo(userId);
        return houseVos;
    }

    @Override
    public int getHistoryCount() {
        String userId = BaseContext.getCurrentId();
        int i = historyMapper.selectHistoryCount(userId);
        return i;
    }
}
